<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_gaji extends MY_Controller {
	
	function __construct() {
		parent::__construct(); 
		$this->load->model('keu_gaji_model', 'Gaji');
		$this->load->model('hrd_spesifikasi_gaji_model', 'SGaji');
		$this->load->model('hrd_karyawan_model', 'Karyawan');
		$this->load->model('hrd_combo_model', 'HrdCombo');
		$this->load->model('keu_store_procedure_model', 'SP');
		$this->secure_session();
	}

	function getdata() {
		echo $this->Gaji->GetJSON(post('idPeriode'));
	}

	function index() {
		// Difine Combo
		$data = $this->kumpulan_kombo();

		$data['title'] = "Pembayaran Gaji Bulanan";
		// $data['data'] = $thiss->Karyawan->GetSingleView('01067');
		$data['data'] = NULL;
		$data['data']['tgl_bayar'] = date('d/m/Y');

		$data['mode'] = "create";
		viewByTemplate('keu_gaji/form', $data, 'form');
	}

	function ajax_post() {
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try {
			if (count( $error->Get() ) > 0) { 
				$is_error = 1; 
			} else {
				$data	= post_by_key(array('tgl_bayar', 'id_periode_fk'));
				
				$li_cek =$this->get_data("SELECT COUNT(1) FROM HRD_GAJI WHERE id_periode_fk='".$data['id_periode_fk']."' AND TGL_BAYAR IS NULL");
				if ($li_cek > 0){
				
					$data['tgl_ubah']	= NOW;
					$data['user_ubah']	= USERID;
					$data['tgl_bayar']  = date_format3($data['tgl_bayar'], 'YYYY-MM-DD');
					$this->Gaji->Update($data, 
										array('id_periode_fk'	=> $data['id_periode_fk']));
	
					$this->SP->SP_GL_GAJI($data['id_periode_fk']);
					$ls_response = "Proses pembentukan jurnal telah selesai!";
				}else{
					$ls_response = "Periode gaji ini sebelumnya telah diproses!";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	private function validation() {
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		// Validation
		// if (post("id_periode_fk") == '') {
		// 	$error->Add("id_periode_fk", "Periode", $tpl_empty);
		// } else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM keu_gaji WHERE id_periode_fk = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("id_periode_fk"))))."'")) > 0) {
		// 	$error->Add("id_periode_fk", "Periode", $tpl_duplicate);
		// }

		return $error;
	}

	function kumpulan_kombo() {
		$data['listPeriode'] = $this->HrdCombo->listPeriode();
		return $data;
	}

	// Buat Detail Gaji
	function get_detail_gaji() {

		echo $this->Gaji->GetDetailJSON(post('idGaji'));
	}

	function form_detail_gaji($idGaji, $namaKaryawan) {
		$data['title'] = "Detail Gaji : ".urldecode($namaKaryawan);
		$data['data'] = NULL;
		$data['idGaji'] = $idGaji;

		$this->load->view('keu_gaji/form_detail', $data);
	}

}